package com.service.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.order.pojo.Order;
import com.service.order.service.OrderService;
import com.service.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-10
 */
@RestController
@RequestMapping("order")
@Api(description = "订单管理")
public class OrderController {



    @Autowired
    private OrderService orderService;

    @GetMapping("{orderId}")
    @ApiOperation("根据订单id,查询订单的详细信息")
    public ResultVo queryById(@PathVariable String orderId){
        return   orderService.queryById(orderId);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除指定的订单信息")
    public ResultVo delete(@PathVariable String id){
        boolean b = orderService.removeById(id);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


    @PutMapping
    @ApiOperation("修改订单信息")
    public ResultVo update(@RequestBody Order order){
        order.setGmtModified(new Date());
        boolean b = orderService.updateById(order);
        if (b){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @PostMapping
    @ApiOperation("添加订单信息,返回订单id")
    public ResultVo save(@RequestBody Order order){

        return orderService.saveOrder(order);
    }

    @GetMapping("queryOrderStatus/{courseId}/{memberId}")
    public ResultVo queryOrderStatus(@PathVariable("courseId") String courseId,@PathVariable("memberId") String memberId){
        System.out.println("courseId = " + courseId + ", memberId = " + memberId);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",memberId);
        Order order = orderService.getOne(queryWrapper);
        if (order.getStatus()==0){
            return ResultVo.error().message("未支付");
        }
        return ResultVo.ok().message("已支付");
    }

}

